package com.example.dingtalkwebproject.controller;

import com.example.dingtalkwebproject.pojo.dArcItem;
import com.example.dingtalkwebproject.pojo.pAccInvGl;
import com.example.dingtalkwebproject.service.dArcItemService;
import com.example.dingtalkwebproject.service.pAccInvGlService;
import com.example.dingtalkwebproject.utils.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/item")
public class getItemController {

    @Autowired
    private dArcItemService dArcItemService;
    @Autowired
    private pAccInvGlService pAccInvGlService;


    /**
     * 根据条码获取当前商品信息
     *
     * @param barcode
     * @return
     */
    @GetMapping("/getItemInfoById/{storeId}/{barcode}/{staffId}")
    public AjaxResult getItemInfoByBarcode(@PathVariable String storeId, @PathVariable String barcode, @PathVariable String staffId) {
        if (StringUtils.isBlank(barcode) || StringUtils.isBlank(storeId)) {
            return AjaxResult.error("参数未被定义!");
        }

        dArcItem dArcItem = dArcItemService.selectItemInfoByBarcode(barcode);
        if (ObjectUtils.isEmpty(dArcItem)) {
            return AjaxResult.error("未获取到商品，请检查条码是否正确，如条码无误，还是扫描不出来，请联系计划部何丽娟去维护采购员信息!");
        }

        // 如果不为空 判断是否还有库存
        if (StringUtils.isNotBlank(staffId)) {
            if (!staffId.startsWith("C")) {
                pAccInvGl pAccInvGl = pAccInvGlService.selectInvGlByItemIdAndStoreId(dArcItem.getItemId(), storeId);
                if (ObjectUtils.isNotEmpty(pAccInvGl)) {
                    if (pAccInvGl.getEndQty() <= 0) {
                        return AjaxResult.error("当前商品在当前门店【" + storeId + "】已经没有库存,不允许进行残次提报!");
                    }
                } else {
                    return AjaxResult.error("当前商品没有库存!");
                }
            }
        }
        return AjaxResult.success(dArcItem);
    }
}
